package com.xuhai.blog19.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhai.blog19.admin.dao.ArticleDao;
import com.xuhai.blog19.admin.entity.ArticleCategoryEntity;
import com.xuhai.blog19.admin.entity.ArticleEntity;
import com.xuhai.blog19.admin.service.ArticleCategoryService;
import com.xuhai.blog19.admin.service.ArticleService;
import com.xuhai.blog19.admin.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author PangJunjie
 * @Date 2023/2/8/008
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {
    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private CommentService commentService;

    @Override
    public IPage<ArticleEntity> getPage(int page, int limit, ArticleEntity article) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(article.getApplyTimeRange())) {
            startTime = article.getApplyTimeRange().split(",")[0];
            endTime = article.getApplyTimeRange().split(",")[1];
        }
        return this.page(new Page<>(page, limit),
                new QueryWrapper<ArticleEntity>()
                        .like(StringUtils.isNotBlank(article.getTitle()), "title", article.getTitle())
                        .eq(StringUtils.isNotBlank(article.getAuthor()), "author", article.getAuthor())
                        .eq(article.getArticleStatus() != null, "article_status", article.getArticleStatus())
                        .eq(article.getStatus() != null, "status", article.getStatus())
                        .between(StringUtils.isNotBlank(article.getApplyTimeRange()), "apply_time", startTime, endTime).orderByDesc("article_id")
        );
    }

    @Override
    public void update(ArticleEntity article) {
        /*状态为发表时设置发布时间*/
        if (new Integer(1).equals(article.getArticleStatus())) {
            article.setApplyTime(new Date());
        }
        /*处理博客信息*/
        this.saveOrUpdate(article);
        /*根据文章id删除所有的分类信息*/
        articleCategoryService.deleteByArticleId(article.getArticleId());
        /*判断是否需要操作分类*/
        if (article.getCategoryIds() != null && article.getCategoryIds().length > 0) {
            /*转化id数组为list集合*/
            List<ArticleCategoryEntity> list = Arrays.stream(article.getCategoryIds()).map(item -> {
                ArticleCategoryEntity ac = new ArticleCategoryEntity();
                ac.setArticleId(article.getArticleId());
                ac.setCategoryId(item);
                return ac;
            }).collect(Collectors.toList());

//            List<ArticleCategoryEntity> list2 = new ArrayList<>();
//            for(Integer categoryId : article.getCategoryIds()){
//                ArticleCategoryEntity ac = new ArticleCategoryEntity();
//                ac.setArticleId(article.getArticleId());
//                ac.setCategoryId(categoryId);
//                list2.add(ac);
//            }
            /*执行批量添加操作*/
            articleCategoryService.saveBatch(list);
        }
    }

    @Override
    public void delete(int articleId) {
        this.removeById(articleId);
        articleCategoryService.deleteByArticleId(articleId);
        commentService.deleteByArticleId(articleId);
    }

    @Override
    public IPage<ArticleEntity> getHotPage(int page) {
        return this.page(new Page<>(page, 5),
                new QueryWrapper<ArticleEntity>()
                        .eq("status", 1)
                        .eq("article_status", 1)
                        .orderByDesc("read_count")
        );
    }

    @Override
    public IPage<ArticleEntity> getArticlePageByCategoryId(int page, int categoryId) {
        return articleDao.getArticlePageByCategoryId(new Page<>(page, 5), categoryId);
    }

    @Override
    public void updateReadCount(Integer articleId) {
        articleDao.updateReadCount(articleId);
    }

    @Override
    public int getCount(ArticleEntity article) {
        return this.count(new QueryWrapper<ArticleEntity>().eq(article.getStatus() != null, "status", article.getStatus()).eq(article.getArticleStatus() != null, "article_status", article.getArticleStatus()));
    }
}
